import 'package:flutter/material.dart';
import 'dart:async';

import 'package:wechat/chat/message_page.dart';
import 'package:wechat/contacts/contacts.dart';
import 'package:wechat/found/found.dart';
import 'package:wechat/personal/persional.dart';

class App extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return MainState();
  }
}

enum ItemType { GroupChat, AddFriends, QrCode, Payments, Help }

_popupMeunItem(BuildContext context, String title,
    {String imagePath, IconData icon}) {
  return PopupMenuItem(
    child: Container(
      child: Row(
        children: <Widget>[
          imagePath != null
              ? Image.asset(
                  imagePath,
                  width: 32,
                  height: 32,
                )
              : SizedBox(
                  width: 32,
                  height: 32,
                  child: Icon(
                    icon,
                    color: Colors.white,
                  ),
                ),
          Container(
            padding: const EdgeInsets.only(left: 20),
            child: Text(
              title,
              style: TextStyle(color: Colors.white),
            ),
          ),
        ],
      ),
    ),
  );
}

class MainState extends State<App> {
  var _currentIndex = 1;

  //聊天页面
  MessagePage message;

  //好友页面
  Contacts contacts;
  Found found;
  Personal me;

  currengPage() {
    switch (_currentIndex) {
      case 0:
        if (message == null) {
          message = MessagePage();
        }
        return message;
      case 1:
        if (contacts == null) {
          contacts = Contacts();
        }
        return contacts;
      case 2:
        if (found == null) {
          found = Found();
        }
        return found;
      case 3:
        if (me == null) {
          me = Personal();
        }
        return me;
      default:
//        ;
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: AppBar(
        title: Text('微信'),
        actions: <Widget>[
          GestureDetector(
            onTap: () {
              Navigator.pushNamed(context, 'search');
            },
            child: Icon(Icons.search),
          ),
          Padding(
            padding: const EdgeInsets.only(left: 30, right: 20),
            child: GestureDetector(
              onTap: () {
                showMenu(
                    context: context,
                    position: RelativeRect.fromLTRB(500, 76, 10, 0),
                    items: <PopupMenuEntry>[
                      _popupMeunItem(context, '发起群聊',
                          imagePath: 'images/icon_menu_group.png'),
                      _popupMeunItem(context, '添加好友',
                          imagePath: 'images/icon_menu_addfriend.png'),
                      _popupMeunItem(context, '扫一扫',
                          imagePath: 'images/icon_menu_scan.png'),
                    ]);
              },
              child: Icon(Icons.add),
            ),
          )
        ],
      ),
      body: currengPage(),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        currentIndex: _currentIndex,
        onTap: ((index) {
          setState(() {
            _currentIndex = index;
          });
        }),
        items: [
          BottomNavigationBarItem(
              backgroundColor: Colors.white,
              icon: _currentIndex == 0
                  ? Image.asset(
                      'images/weixin_pressed.png',
                      width: 32,
                      height: 28,
                    )
                  : Image.asset(
                      'images/weixin_normal.png',
                      width: 32,
                      height: 28,
                    ),
              title: Text(
                '微信',
                style: TextStyle(
                    color: _currentIndex == 0
                        ? Color(0xff46c01b)
                        : Color(0xff999999)),
              )),
          BottomNavigationBarItem(
              icon: _currentIndex == 1
                  ? Image.asset(
                      'images/contact_list_pressed.png',
                      width: 32,
                      height: 28,
                    )
                  : Image.asset(
                      'images/contact_list_normal.png',
                      width: 32,
                      height: 28,
                    ),
              title: Text(
                '通讯录',
                style: TextStyle(
                    color: _currentIndex == 1
                        ? Color(0xff46c01b)
                        : Color(0xff999999)),
              )),
          BottomNavigationBarItem(
              icon: _currentIndex == 2
                  ? Image.asset(
                      'images/find_pressed.png',
                      width: 32,
                      height: 28,
                    )
                  : Image.asset(
                      'images/find_normal.png',
                      width: 32,
                      height: 28,
                    ),
              title: Text(
                '发现',
                style: TextStyle(
                    color: _currentIndex == 2
                        ? Color(0xff46c01b)
                        : Color(0xff999999)),
              )),
          BottomNavigationBarItem(
              icon: _currentIndex == 3
                  ? Image.asset(
                      'images/profile_pressed.png',
                      width: 32,
                      height: 28,
                    )
                  : Image.asset(
                      'images/profile_normal.png',
                      width: 32,
                      height: 28,
                    ),
              title: Text(
                '我',
                style: TextStyle(
                    color: _currentIndex == 3
                        ? Color(0xff46c01b)
                        : Color(0xff999999)),
              )),
        ],
      ),
    );
  }
}
